var wechatMsgConstruct = require('./wechatMsgConstruct');
module.exports = function (req, res, next) {
    console.log('服务器配置');
    if (req.wechatMsg.MsgType == "text") {
        switch (true) {
            case /配置-[0-9]{1,2}/.test(req.wechatMsg.Content):
            case /详细信息-[0-9]{1,2}/.test(req.wechatMsg.Content):
            case /服务器-[0-9]{1,2}/.test(req.wechatMsg.Content):
            case /节点-[0-9]{1,2}/.test(req.wechatMsg.Content):
                if(req.wechatUserStatus){
                    console.log('接受命令');
                    getNodeConfig(req, res, next);
                }else {
                    var chat = {
                        Type: "text",
                        Content: require('../config/config').unRegistered
                    };
                    // 最后一步需要信息,并关闭数据库连接
                    req.mysqlConnection.end();
                    res.send(wechatMsgConstruct(req.wechatMsg, chat));
                }

                break;
            default:
                next();
                break;
        }
    } else {
        next()
    }

    function getNodeConfig(req, res, next) {
        var nodeConfigStr ="服务器配置:\n";
        var node_id = req.wechatMsg.Content.split('-')[1];//切割字符串获取编号
        var query = req.mysqlConnection.query("SELECT server FROM ss_node where id = " + node_id);//炒作数据库
        query
            .on('error', function (err) {
                console.log('数据库连接失败', err)
            })
            .on('result', function (row) {
                nodeConfigStr += "服务器ip：" + row.server + "\n";
                nodeConfigStr += "端口：" + req.wechatUserInfo.id + "\n";
                nodeConfigStr += "密码：" + req.wechatUserInfo.pass + "\n";
                nodeConfigStr += "加密/算法：" + req.wechatUserInfo.method + "\n";
                nodeConfigStr += "协议：" + req.wechatUserInfo.protocol + "\n";
                nodeConfigStr += "协议参数：空\n";
                nodeConfigStr += "混淆：" + req.wechatUserInfo.obfs + "\n";
                nodeConfigStr += "混淆参数：空\n";
            })
            .on('end', function () {
                if (nodeConfigStr =="服务器配置:\n"){
                    var chat = {
                        Type: "text",
                        Content: nodeConfigStr+"数据异常，无法读取您的配置信息\n"
                    };
                    // 最后一步需要信息,并关闭数据库连接
                    req.mysqlConnection.end();
                    res.send(wechatMsgConstruct(req.wechatMsg, chat));
                }else {
                    var chat = {
                        Type: "text",
                        Content: nodeConfigStr
                    };
                    // 最后一步需要信息,并关闭数据库连接
                    req.mysqlConnection.end();
                    res.send(wechatMsgConstruct(req.wechatMsg, chat));
                }

            });
    }

};